#ifndef UART_H_
#define UART_H_

/*******************************************************************************
 * File name: 		uart.h
 * Description: 
 * Project: 		uart
 * Target: 			LPC2478
 * Compiler: 		arm-none-eabi-gcc
 * Date: 			09-09-2011
 * Author: 			kuba
 * Based on: 		---
 ******************************************************************************/

/*==============================================================================
 Includes
==============================================================================*/
# include <stdbool.h>
# include "typedef.h"
# include "hdr_scb.h"
/*==============================================================================
 Defines
==============================================================================*/
# define UART_TEST                  1

# define INTRPT_RX_TRIG_LVL_1CHAR   1
# define INTRPT_RX_TRIG_LVL_4CHAR   4
# define INTRPT_RX_TRIG_LVL_8CHAR   8
# define INTRPT_RX_TRIG_LVL_14CHAR  14
/*==============================================================================
 Globals
==============================================================================*/
enum uartNo {
	uart0,
	uart1,
	uart2,
	uart3
};

enum baud {
	baud50 		= 50,
	baud75 		= 75,
	baud110 		= 110,
	baud134 		= 134,
	baud150 		= 150,
	baud200 		= 200,
	baud300 		= 300,
	baud600 		= 600,
	baud1200 	= 1200,
	baud1800 	= 1800,
	baud2400 	= 2400,
	baud4800 	= 4800,
	baud9600 	= 9600,
	baud19200 	= 19200,
	baud38400 	= 38400,
	baud57600 	= 57600,
	baud115200 	= 115200,
	baud230400 	= 230400,
	baud460800 	= 460800,
	baud576000 	= 576000
};

enum wordLen {
	word5BIT = U_LCR_WORD_LENGTH_SELECT_5BIT,			/* 5 bit character length */
	word6BIT = U_LCR_WORD_LENGTH_SELECT_6BIT,			/* 6 bit character length */
	word7BIT = U_LCR_WORD_LENGTH_SELECT_7BIT,			/* 7 bit character length */
	word8BIT = U_LCR_WORD_LENGTH_SELECT_8BIT			/* 8 bit character length */
};

enum stopBit {
	stop1BIT = U_LCR_STOP_BIT_SELECT_1BIT,				/* 1 stop bit */
	stop2BIT = U_LCR_STOP_BIT_SELECT_2BIT,				/* 2 stop bits */
};

enum parity {
	parityDISABLE 	= 0,										/* parity disabled */
	parityENABLE 	= U_LCR_PARITY_ENABLE				/* parity enabled */
};

enum paritySel {
	parityODD 		= U_LCR_PARITY_SELECT_ODD, 		/* odd parity */
	parityEVEN 		= U_LCR_PARITY_SELECT_EVEN, 		/* odd parity */
	parityFORCED1 	= U_LCR_PARITY_SELECT_F1, 			/* forced "1" stick parity */
	parityFORCED0 	= U_LCR_PARITY_SELECT_F0 			/* forced "2" stick parit */
};

enum breakCtrl {
	breakCTRL_DISABLE = 0,
	breakCTRL_ENABLE = U_LCR_BREAK_CONTROL
};

enum fifo {
	fifoDISABLE = 0,
	fifoENABLE 	= U_FCR_FIFO_ENABLE,							/* enable fifo */
};

enum intrpt {
	intrptRX 				= U_IER_INTERRUPT_EN_RBR,				/* enable RX interrupt */
	intrptTX 				= U_IER_INTERRUPT_EN_THRE,				/* enable TX interrupt */
	intrptRX_STATUS 		= U_IER_INTERRUPT_EN_RX_STATUS,		/* enable RX status interrupt */
	intrptAUTO_BAUD_END	= U_IER_INTERRUPT_EN_ABAUD_END,		/* end of auto-baut interrupt */
	intrptAUTO_BAUD_TO	= U_IER_INTERRUPT_EN_ABAUD_TIMEOUT 	/* auto baud timeout */
};

/* NOTE: there is no hardware tigger level for TX, but it is implemented.
 * It takes advantage of the fact that TX fifo can be constantly filled with
 * characters and if filling ends, then hardware generate interrupts. So, here
 * software pushes required number of chars and waits- during the delay TX
 * interrupt occurs. Take a look at uartSend function.
 */
enum intrptTxTrig {
   intrptTX_TRIG_1CHAR = 1,
   intrptTX_TRIG_2CHAR = 2,
   intrptTX_TRIG_3CHAR = 3,
   intrptTX_TRIG_4CHAR = 4,
   intrptTX_TRIG_5CHAR = 5,
   intrptTX_TRIG_6CHAR = 6,
   intrptTX_TRIG_7CHAR = 7,
   intrptTX_TRIG_8CHAR = 8,
   intrptTX_TRIG_9CHAR = 9,
   intrptTX_TRIG_10CHAR = 10,
   intrptTX_TRIG_11CHAR = 11,
   intrptTX_TRIG_12CHAR = 12,
   intrptTX_TRIG_13CHAR = 13,
   intrptTX_TRIG_14CHAR = 14,
   intrptTX_TRIG_15CHAR = 15,
   intrptTX_TRIG_16CHAR = 16
};

enum intrptRxTrig {
	intrptRX_TRIG_1CHAR = INTRPT_RX_TRIG_LVL_1CHAR,
	intrptRX_TRIG_4CHAR = INTRPT_RX_TRIG_LVL_4CHAR,
	intrptRX_TRIG_8CHAR = INTRPT_RX_TRIG_LVL_8CHAR,
	intrptRX_TRIG_14CHAR = INTRPT_RX_TRIG_LVL_14CHAR
};

struct buffers {
	uint8 *txBuff;
	uint32 txBuffSize;
	uint8 *rxBuff;
	uint32 rxBuffSize;
};

struct uart {
	enum uartNo 		uartNo;
	enum baud 			baud;
	enum wordLen 		wordLen;
	enum stopBit 		stopBit;
	enum parity 		parity;
	enum paritySel 	paritySel;
	enum breakCtrl 	breakCtrl;
	enum fifo 			fifo;
	uint32 			   intrpt;
	enum intrptTxTrig intrptTxTrig;
	enum intrptRxTrig intrptRxTrig;
	//uint8 irda;
	struct buffers 	buffers;
};


/*==============================================================================
 Global function prototypes
==============================================================================*/
bool uartInit 			(const struct uart *uart);
bool uartSend			(const struct uart *uart);
void 	uartPutChar 	(char character);
uint8 uartGetChar 	(void);


/******************************************************************************
* END OF FILE
******************************************************************************/

#endif /* UART_H_ */
